package com.jiyun.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.pagehelper.PageInfo;
import com.jiyun.model.sign.PendingSigning;
import com.jiyun.model.sign.SigningInfo;
import com.jiyun.model.sign.dto.PendingSigningDto;
import com.jiyun.result.Result;
import com.jiyun.service.IPendingSigningService;
import com.jiyun.vo.sign.PendingSigningVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 【-PendingSigning】操作接口
 *
 * @author ???
 * @since 2024-09-27
 */
@RestController
@RequestMapping("/pendingSigning")
@Api(tags = "操作接口")
@CrossOrigin
public class PendingSigningController {

    @Autowired
    public IPendingSigningService pendingSigningService;

    /**
     * 接口：添加
     * @param pendingSigning 要添加的
     * @return Result 响应给前端
     */
    @PostMapping
    @ApiOperation("添加" )
    public Result save(@RequestBody PendingSigning pendingSigning) {
        pendingSigningService.save(pendingSigning);
        return Result.ok();
    }

    /**
     * 接口：根据Id删除
     * @param id 的Id
     * @return Result 响应给前端
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("根据Id删除")
    public Result remove(@PathVariable("id" ) Long id) {
        pendingSigningService.removeById(id);
        return Result.ok();
    }

    /**
     * 接口：修改
     * @param pendingSigning 要修改的数据
     * @return Result 响应给前端
     */
    @PutMapping("update")
    @ApiOperation("修改")
    public Result updateById(@RequestBody PendingSigning pendingSigning) {
        pendingSigningService.updateById(pendingSigning);
        return Result.ok();
    }


    /**
      * 根据Id查询单个
     */
    @GetMapping("/get/{id}")
    @ApiOperation("根据Id查询单个")
    public Result getById(@PathVariable Long id)  {
        PendingSigning one = pendingSigningService.getById(id);
        return Result.ok(one);
    }
    @PostMapping("/findAllPendingSigning/{flag}")
    @ApiOperation("查询所有+分页+条件查询")
    public Result findAllPendingSigning(@RequestBody PendingSigningDto pendingSigningDto, @PathVariable("flag") Integer flag){
        //待签约处理
        PageInfo<PendingSigningVo> pageInfo=pendingSigningService.findAllPendingSigning(flag,pendingSigningDto);
        return  Result.ok(pageInfo);
    }
    @GetMapping("/exportExcel")
    @ApiOperation("导出复选框选择的数据")
    public Result exportExcel(Integer[] arr) throws IOException {
        List<PendingSigningVo> list = pendingSigningService.ExceptionByids(Arrays.asList(arr));
        for (PendingSigningVo pendingSigning : list) {
            System.out.println(pendingSigning);
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), PendingSigningVo.class, list);
        String url="D:\\xls";
        File file = new File(url);
        if (!file.exists()){
            file.mkdir();
        }
        workbook.write(new FileOutputStream(new File(url+"\\pendingSigning.xls")));
        workbook.close();
        return Result.ok();
    }

    @GetMapping("/ApprovedByreView")
    @ApiOperation("审核通过")
    public Result ApprovedByreView(Integer pendingSigningId){
        Boolean result=pendingSigningService.ApprovedByreView(pendingSigningId);
        if (result){
            return Result.ok();
        }
        return Result.fail();
    }
    @PostMapping("/reject")
    @ApiOperation("审核驳回+原因")
    public Result reject(@RequestBody SigningInfo signingInfo){
        System.out.println(signingInfo);
        Boolean result=pendingSigningService.reject(signingInfo.getPendingSigningId(),signingInfo.getReasonForRejection());
        if (result){
            return Result.ok();
        }
        return Result.fail();
    }
}
